package lock;

/**
 * @author fei
 * @date 2023/1/8 16:43
 * @description 可重入锁 同步块 和 同步方法
 */
public class ReEntryLockDemo {

    /**
     * 同步代码块, 对比一下之前的 8锁案例，第1和2
     */
    public synchronized void m1(){
        System.out.println(Thread.currentThread().getName() + "\t -----come in");
        m2();
        System.out.println(Thread.currentThread().getName() + "\t -----end in");
    }

    public synchronized void m2(){
        System.out.println(Thread.currentThread().getName() + "\t -----come in");
        m3();
    }

    public synchronized void m3(){
        System.out.println(Thread.currentThread().getName() + "\t -----come in");
    }

    public static void main(String[] args) {
        ReEntryLockDemo reEntryLockDemo = new ReEntryLockDemo();
        new Thread(() -> {
            reEntryLockDemo.m1();
        }, "t1").start();
    }

    /**
     * 同步块
     */
    private static void reEntryM1() {
        final Object object = new Object();
        new Thread(() -> {
            synchronized (object){
                System.out.println(Thread.currentThread().getName() + "\t -----外层调用");
                synchronized (object){
                    System.out.println(Thread.currentThread().getName() + "\t -----中层调用");
                    synchronized (object){
                        System.out.println(Thread.currentThread().getName() + "\t -----内层调用");
                    }
                }
            }
        }, "t1").start();
    }

}
